# -*- coding:utf-8 -*-
import json
import os
import re

from selenium import webdriver
from appUtils import browser_changeTopWindow, waitInfiniti, getWebDriver, logProxyContent
from browsermobproxy import Server

LoginName = "hiiri2017@163.com"
LoginPd = "Hagongda123"


def browser_doWait(browser):
    import time
    browser.implicitly_wait(10)
    time.sleep(10)
    pass


def loadData(webProxyDriver):
    (browser, proxyServer, _) = webProxyDriver
    result = [];
    # 跳到登录页面
    browser.get("https://appstoreconnect.apple.com/")
    browser_doWait(browser)
    try:
        # 输入用户名
        iframe = browser.find_element_by_id("aid-auth-widget-iFrame");
        browser.switch_to.frame(iframe)
        browser.find_element_by_id("account_name_text_field").send_keys(LoginName)
        browser.find_element_by_class_name("icon_sign_in").click()
        browser_doWait(browser)
        # 输入密码
        browser.find_element_by_id("password_text_field").send_keys(LoginPd)
        browser.find_element_by_class_name("icon_sign_in").click()
        browser_doWait(browser)
    except:
        pass
    # 跳到下载量页面
    browser.get("https://reportingitc2.apple.com/sales.html")
    browser_doWait(browser)

    # 查询最近30天下载量
    browser_changeTopWindow(browser)
    browser.find_element_by_class_name("date-selector-inner")
    browser.find_element_by_css_selector("div[class='date-selector-inner'] div[class='button']").click()
    dateRangItem = browser.find_element_by_css_selector("div[class='range-selector-wrap']")
    dateRangItem = dateRangItem.find_elements_by_tag_name("li")
    dateRangItem[2].click()
    browser_doWait(browser)
    # 解析数据
    # proxyClient.wait_for_traffic_to_stop(1, 60)
    # # logProxyContent(proxyClient)
    # # 处理下载量
    # resStr = proxyClient.har['log']['entries'][0]["response"]['content']["text"]
    # jsonObj = json.loads(resStr)
    # for item in jsonObj["result"][0]["data"]:
    #     result.append((item["day"], item["units_utc"]))
    # proxyServer.stop()
    # browser.quit()
    print("apple",result)
    return result
